'''
Author: 梦付千秋星垂野 465943794@qq.com
Date: 2022-06-27 09:34:45
LastEditors: 梦付千秋星垂野 465943794@qq.com
LastEditTime: 2022-07-04 11:17:55
FilePath: /base_machinelearning/Smartcity_CNN_Fish/algorithm/connect_db.py
Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
'''
import pymysql
import pandas as pd
import numpy as np
from sklearn.preprocessing import MinMaxScaler
def create_dataset(dataset, label):
    dataX, dataY = [], []
    for i in range(len(dataset)):
        a = dataset[i]
        dataX.append(a)
        dataY.append(label[i])
    return np.array(dataX), np.array(dataY)
def readData(feature,Y, distinct):
    # 参数1:主机名或工P地址; 参数2:用户名; 参数3:密码; 参数4: 数据库名称
    # db = pymysql.connect(host="localhost", user="root",password="password", database="smart_cim")
    #db = pymysql.connect(host="localhost",port=3307, user="app_user",password="123456", database="smart_cim")
    db = pymysql.connect(host="localhost",port=3306, user="app_user",password="123456", database="smart_cim")
    sql = 'select * from ' + distinct
    dataframes = pd.read_sql(sql, db)
    dataframe = dataframes[feature]
    dataY = dataframes[Y]
    datasets = dataframe.values
    dataY =dataY.values
    dataset = datasets.astype('float32')
    scalerX = MinMaxScaler(feature_range=(0, 1))
    dataset = scalerX.fit_transform(dataset)
    scalerY = MinMaxScaler(feature_range=(0, 1))
    dataY =scalerY.fit_transform(dataY.reshape(-1,1))
    #print("scalerY.min_:",scalerY.min_)

    '''
    # # 缩小train的比例
    # train_size = int(len(dataset) * 0.8)
    # dataset= dataset[train_size:]
    # dataY= dataY[train_size:]
    # print(train_size)
    '''
    
    # trainlist = dataset[:train_size]
    # testlist = dataset[train_size:]
    # trainLabel = dataY[:train_size]
    # testLabel = dataY[train_size:]
    # trainX, trainY = create_dataset(trainlist, trainLabel)
    # testX, testY = create_dataset(testlist, testLabel)
    trainX, trainY = create_dataset(dataset, dataY)
    # 关闭数据库连接
    db.close()
    return trainX,trainY,scalerX,scalerY